﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DotNetDream.Models
{
    public partial class Blog
    {
        #region Helper classes
        /// <summary>
        /// Blog archive year
        /// </summary>
        public class ArchiveYear
        {
            #region Property
            public int Year { get; set; }
            public int[] Month { get; set; }
            /// <summary>
            /// Get the total year entries count
            /// </summary>
            public int EntryCount
            {
                get
                {
                    int count = 0;
                    foreach (int monthCount in Month)
                    {
                        count += monthCount;
                    }
                    return count;
                }
            }
            #endregion

            /// <summary>
            /// Construct an archive year
            /// </summary>
            /// <param name="year"></param>
            public ArchiveYear(int year)
            {
                Year = year;
                Month = new int[12];
            }
        }
        #endregion

        #region Properties
        public List<ArchiveYear> Archives { get; set; }
        public ICollection<BlogEntry> BlogEntries { get; set; }
        public int CurrentPage { get; set; }
        public int Year { get; set; }
        public int Month { get; set; }
        #endregion

        #region Pulic helpers
        //TODO: Need a better way of handle archives -> An Archive table instead of querying the whole DB
        /// <summary>
        /// Build the archives list for the Blog
        /// </summary>
        /*public void BuildArchives()
        {
            this.Archives = new List<Blog.ArchiveYear>();
            //Get archive years
            var years = (
                from entry
                in this.BlogEntries
                select entry.Created.Year).Distinct();

            //Each year process archive month entries count
            foreach (var year in years)
            {
                //Create year archive
                Blog.ArchiveYear archiveYear = new Blog.ArchiveYear((int)year);
                //Populate archive month
                for (int i = 0; i < archiveYear.Month.Length; i++)
                {
                    archiveYear.Month[i] = this.BlogEntries.Count(e => e.Created.Month == i + 1 && e.Created.Year == archiveYear.Year);
                }
                //Then add to the archive list
                this.Archives.Add(archiveYear);
            }
        }*/


        #endregion
    }
}